package com.iocup.keybastion.utils;


import com.iocup.keybastion.common.AuthConstant;
import com.iocup.keybastion.context.WebContextHolder;

import java.lang.reflect.Method;
import java.util.Optional;

/**
 * @author xyjxust
 * @create 2022/3/3 10:42
 **/
public class NameUtils {

    public static String buildMethodFullPath(Method method) {
        return String.format("%s.%s", method.getDeclaringClass().getName(), method.getName());
    }

    public static String getClientName(WebContextHolder webContextHolder) {
        Optional<String> clientName = webContextHolder.getRequestHeader(AuthConstant.LOGIN_CLIENT_NAME);
        if (!clientName.isPresent()) {
            clientName = webContextHolder.getRequestParameter(AuthConstant.LOGIN_CLIENT_NAME);
        }
        if (!clientName.isPresent()) {
            clientName = Optional.of(AuthConstant.DEFAULT_CLIENT_NAME);
        }
        return clientName.get();
    }

    public static String getAuthName(WebContextHolder webContextHolder) {
        return webContextHolder.getRequestHeader(AuthConstant.AUTH_TYPE, AuthConstant.DEFAULT_AUTH_TYPE);
    }


    public static String getDevice(WebContextHolder webContextHolder) {
        return webContextHolder.getRequestHeader(AuthConstant.DEVICE_NAME, AuthConstant.DEFAULT_DEVICE_NAME);
    }


    public static String buildStorageKey(WebContextHolder webContextHolder) {
        return String.format("%s%s", AuthConstant.SESSION_2_REQUEST_KEY, NameUtils.getDevice(webContextHolder));
    }
}
